<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class ShopTechnicianController extends CommonController {

    public function index()
    {
        $shop_id = I('shop_id');
        $map = [];
        if($shop_id){
            $map['shop_id'] = $shop_id;
        }
        list($list, $page) = $this->lists('ShopTechnician', $map, 'id asc');
        $shop_list = M('shop')->where(['status'=>1])->getField('id,shopname');
        if($list){
            foreach ($list as $key => &$row) {
                $row['shopname'] = isset($shop_list[$row['shop_id']]) ? $shop_list[$row['shop_id']] : '';
            }
        }
        $this->assign('shop_list', $shop_list);
        $this->assign('shop_id', $shop_id);
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function show()
    {
        $id = I('id');
        $info = M('ShopTechnician')->where(['id'=>$id])->find();
        if(empty($info)){
            if(IS_POST){
                $this->ajaxReturn(array('s'=>false,'m'=>'信息不存在或已被删除!'));
            }
            $this->error('信息不存在或已被删除!');
        }
        if(IS_POST){
            $data = I('post.');

            $model = M('ShopTechnician');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        
        $id = I('id');
        $res = M('ShopTechnician')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}